package com.game.controller;

import com.game.chat_room.ServerListener;
import com.game.pojo.vo.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  聊天室 前端控制器
 * </p>
 *
 * @author lvhao
 * @since 2020-07-25
 */
/*@CrossOrigin
@RestController
@RequestMapping("/api")
@Api(tags = "聊天室")*/
public class ChatRoomController {
    @Autowired
    private ServerListener serverListener;

    @GetMapping("/chatroom/check")
    @ApiOperation("查看聊天服务器状态")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 202, message = "查询失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result check_chat_server(){
        if(serverListener.isRunning()){
            return new Result(200, "查询成功", "聊天服务已启动");
        }else{
            return new Result(200, "查询成功", "聊天服务已关闭");
        }
    }

    @PostMapping("/chatroom/run")
    @ApiOperation("聊天服务器启动")
    @ApiResponses({
            @ApiResponse(code = 200, message = "启动成功"),
            @ApiResponse(code = 202, message = "启动失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result run_chat_server(int port){
        serverListener.setRunning(true);
        if(serverListener.isRunning()){
            return new Result(200, "启动成功", "聊天服务已启动");
        }else{
            return new Result(200, "启动失败", "聊天服务未启动");
        }
    }

    @PostMapping("/chatroom/stop")
    @ApiOperation("聊天服务器关闭")
    @ApiResponses({
            @ApiResponse(code = 200, message = "关闭成功"),
            @ApiResponse(code = 202, message = "关闭失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result stop_chat_server(){
        serverListener.setRunning(false);
        if(serverListener.isRunning()){
            return new Result(200, "关闭失败", "聊天服务已启动");
        }else{
            return new Result(200, "关闭成功", "聊天服务已关闭");
        }
    }

}
